package com.spring.schooltest.scheduling;

import java.util.List;

public class Simulate {
    public void simulateCPU(List<MyProcess> processList) {
        // 先将进程优先级排序
        Priority prioritySort = new Priority();
        List<MyProcess> sortProcessList = prioritySort.ProcessSort(processList);

        // 遍历排序后的进程集合
        System.out.println("=============进程就绪=============");
        System.out.println("进程名      优先级      需要的运行时间");
        for (int i = 0; i < sortProcessList.size(); i++) {
            String name = sortProcessList.get(i).getName();
            int priority = sortProcessList.get(i).getPriority();
            int serveTime = sortProcessList.get(i).getServeTime();
            System.out.println(name + "           " + priority + "            " + serveTime);
        }
        System.out.println("运行进程: ");

        CPU cpu = new CPU();
        for (MyProcess readyProcess : sortProcessList ){
            // 进程第一次进入CPU
            MyProcess process = cpu.run(readyProcess);

            // 进程剩余运行时间
            int serveTime = process.getServeTime();

            sortProcessList.remove(process);
            if (serveTime > 0) {
                sortProcessList.add(process);
            }

            if (!sortProcessList.isEmpty()) {
                // 如果该进程集合不为空，继续运行
                simulateCPU(sortProcessList);
            }else {
                System.out.println("==========运行结束==========");
            }
            if (!sortProcessList.isEmpty()) {
                break;
            }
        }
    }
}
